package mpi;
// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.6B4161CA-CCB2-8A59-2E81-0EDDE6E2C8E4]

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Date;

// </editor-fold> 
public class MPICmd implements ServerCommand, Serializable {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.EFFDDD3A-8771-AE14-6727-DF03B98899F3]
    // </editor-fold> 
    private static int indexCmd = 0;
    public MPIController controller = null;

    public static int getIndex() {
        return indexCmd++;
    }
    private int id;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.D6D8EA90-9D07-9D2A-5959-68638A10F281]
    // </editor-fold> 
    private String name = null;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.DCCF276F-36BC-C240-49FB-A60EB1ED360B]
    // </editor-fold> 
    private String fileName = null;
    private String fileNameResult = null;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.8E84D53E-49F8-32A9-338C-A93F3B48B6C1]
    // </editor-fold> 
    public MPICmd(String name, MPIController controller) {
        this.id = MPICmd.getIndex();
        this.setName(name);
        this.controller = controller;
        Date date = new Date();
        date.getTime();
        this.fileNameResult = name + "_" + System.nanoTime() + ".res";
    }
    
    public MPICmd(String name, String fileName, MPIController controller) {

        this.id = MPICmd.getIndex();
        this.setName(name);
        this.setFileName(fileName);
        this.controller = controller;
    }

    @Override
    public String toString() {
        return "command id = " + this.getId() + " Name = " + this.getName();
    //+ "\nFile Name = " +this.getFileName();
    }
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.8A7799CC-52AF-EA2D-BDA0-05EAB28A9D04]
    // </editor-fold> 

    public boolean execCommand() {

        FileOutputStream file;
        InputStream console;
        int c;

        try {
            file = new FileOutputStream(this.fileNameResult);
            if (this.fileName != null) {
                console = Runtime.getRuntime().exec(this.fileName).getInputStream();

            } else {
                console = Runtime.getRuntime().exec(this.getName()).getInputStream();
            }
            while ((c = console.read()) != -1) {
                file.write(c);
            }
            console.close();
            file.close();
        } catch (Exception e) {
            System.err.println("Can't " + this.toString());
            System.err.println(e.getStackTrace());
        }

        return true;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.BB81E0ED-6CFF-8015-10C0-B9BCFA26F8A8]
    // </editor-fold> 
    public String getFileName() {
        return fileName;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.1F98D317-590D-1640-8A9B-A36D5B2E9D93]
    // </editor-fold> 
    public void setFileName(String val) {
        this.fileName = val;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.E9FE0785-15D5-171D-187B-0BA42B4C6A14]
    // </editor-fold> 
    public int getId() {
        return id;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.7EF7F77B-4879-0047-B224-D195C6F0321F]
    // </editor-fold> 
    public void setId(int val) {
        this.id = val;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.BD65E320-0F4C-6ED1-F0E4-BA4F41CA5D2C]
    // </editor-fold> 
    public String getName() {
        return name;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.48D092DB-1453-A639-3D4F-CE3BCB3CE18A]
    // </editor-fold> 
    public void setName(String val) {
        this.name = val;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.A095EDF6-19BC-CBA7-6D2A-015FC819BC3B]
    // </editor-fold> 
    public String setDefaultFileName() {
        return null;
    }

    public String getFileNameResult() {
        return fileNameResult;
    }

    public void setFileNameResult(String fileNameResult) {
        this.fileNameResult = fileNameResult;
    }
}

